package com.ruoyi.onway.controller;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.report.domain.ReportPlan;
import com.ruoyi.report.domain.ReportPlanDetail;
import com.ruoyi.report.mapper.ReportPlanMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.onway.domain.ReportOnway;
import com.ruoyi.onway.service.IReportOnwayService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 在途油信息Controller
 * 
 * @author ruoyi
 * @date 2024-10-12
 */
@RestController
@RequestMapping("/onway/onway")
public class ReportOnwayController extends BaseController
{
    @Autowired
    private IReportOnwayService reportOnwayService;

    @Autowired
    private ReportPlanMapper reportPlanMapper;

    /**
     * 查询在途油信息列表
     */

    @GetMapping("/list")
    public TableDataInfo list(ReportOnway reportOnway)
    {
        startPage();
        List<ReportOnway> list = reportOnwayService.selectReportOnwayList(reportOnway);
        for (ReportOnway onway : list) {

        }
        return getDataTable(list);
    }

    /**
     * 导出在途油信息列表
     */

    @Log(title = "在途油信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ReportOnway reportOnway, HttpServletResponse response)
    {
        List<ReportOnway> list = reportOnwayService.selectReportOnwayList(reportOnway);
        ExcelUtil<ReportOnway> util = new ExcelUtil<ReportOnway>(ReportOnway.class);
        util.exportExcel(response,list, "炼厂分配信息");
    }

    /**
     * 获取在途油信息详细信息
     */

    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(reportOnwayService.selectReportOnwayById(id));
    }

    /**
     * 新增在途油信息
     */

    @Log(title = "在途油信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ReportOnway reportOnway)
    {
//        this.check(reportOnway);
        return toAjax(reportOnwayService.insertReportOnway(reportOnway));
    }

    /**
     * 修改在途油信息
     */

    @Log(title = "在途油信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ReportOnway reportOnway)
    {
//        this.check(reportOnway);
        return toAjax(reportOnwayService.updateReportOnway(reportOnway));
    }


//    public Boolean check(ReportOnway reportOnway){
//        //查询出 该计划 给该炼厂一共分配了多少
//        Long planId = reportOnway.getPlanId();
//        Long refineryId = reportOnway.getRefineryId();
//        ReportPlan reportPlan = reportPlanMapper.selectReportPlanById(planId);
//        List<ReportPlanDetail> reportPlanDetailList = reportPlan.getReportPlanDetailList();
//        if (CollectionUtil.isEmpty(reportPlanDetailList)){
//            throw new RuntimeException("该采购计划尚未给任何炼厂分配资源,请先分配");
//        }
//        List<ReportPlanDetail> collect = reportPlanDetailList.stream()
//                .filter(detail -> detail.getRefineryId().equals(refineryId)) // 过滤出指定 refineryId 的详情
//                .collect(Collectors.toList());
//
//        BigDecimal reduce = collect.stream()
//                .map(ReportPlanDetail::getQuantity) // 获取 quantity 字段
//                .reduce(BigDecimal.ZERO, BigDecimal::add);
//        BigDecimal sendWeight = reportOnway.getSendWeight();
//        if (sendWeight.compareTo(reduce)>0){
//            throw new RuntimeException("当前分配源大于采购计划分配额度,请减少配送量");
//        }
//
//
//        return true;
//    }

    /**
     * 删除在途油信息
     */
    @PreAuthorize("@ss.hasPermi('onway:onway:remove')")
    @Log(title = "在途油信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(reportOnwayService.deleteReportOnwayByIds(ids));
    }
}
